<?php

namespace App\Models;

use houdunwang\arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $guarded=['attrs'];

    // 自关联获取父类数据
    public function category(){
        return $this->belongsTo(Category::class,'pid','id');
    }

    // 判断是否有子类
    public function hasChild($model)
    {
        // 获取所有属性数据
        $data = Category::get()->toArray();
//        dd($model->id);
        return Arr::hasChild($data, $model['id'], 'pid');
    }


    // 递归查询分类结构
    public function getSon($cate,$name='son',$pid=0){
        // 定义空数组用来接收每一级的分类数据
        $arr=[];
        // 调用递归
        foreach ($cate as $v){
            if($v->pid==$pid){
                $v->$name=$this->getSon($cate,$name,$v->id);
                $arr[]=$v->toArray();
            }
        }
        return $arr;
    }

    // 获取每一级分类下的所有数据
    public function getCategoryData($attrArr){
        // 获取子分类的数据
        $son=Category::where('pid',$this->id)->first();
        // 判断是否有子集
        if($son){
            // 判断是否有子集的子集
            $secondson=Category::where('pid',$son->id)->first();
            if($secondson){
                // 获取二级分类的id
                $secondcategorys=Category::where('pid',$this->id)->pluck('id');
                // 获取三级分类的id
                $thirdcategorys=Category::whereIn('pid',$secondcategorys)->pluck('id');
                if($attrArr){
                    // 同理获取所有三级商品id
                    $goodsIds=Goods::whereIn('thirdCategory',$thirdcategorys)->pluck('id');
                    $goods=$this->selectGoods($goodsIds,$attrArr);
                }else{
                    // 获取分类id对应商品数据
                    $goods=Goods::whereIn('thirdCategory',$thirdcategorys)->get();
                }
                // 获取分类id对应的属性id
                $attrIds=CategoryAttr::whereIn('category_id',$thirdcategorys)->pluck('attr_id');
                // 获取所有属性数据
                $attrs=Attr::whereIn('id',$attrIds)->get();
            }else{
                // 获取所有3级商品的分类id
                $categorys=Category::where('pid',$this->id)->pluck('id');
                if($attrArr){
                    // 获取所有3级商品的id
                    $goodsIds=Goods::whereIn('thirdCategory',$categorys)->pluck('id');
                    $goods=$this->selectGoods($goodsIds,$attrArr);
                }else{

                    $goods=Goods::whereIn('thirdCategory',$categorys)->get();
                }
                $attrIds=CategoryAttr::whereIn('category_id',$categorys)->pluck('attr_id');
                $attrs=Attr::whereIn('id',$attrIds)->get();
            }
        }else{
            // 判断是否穿属性参数
            if($attrArr){
                // 获取所有商品id
                $goodsIds=Goods::where('thirdCategory',$this->id)->pluck('id');
                // 调用方法,筛选包含当前属性的商品数据
                $goods=$this->selectGoods($goodsIds,$attrArr);
            }else{
                $goods=Goods::where('thirdCategory',$this->id)->get();
            }


            $attrIds=CategoryAttr::where('category_id',$this->id)->pluck('attr_id');
            $attrs=Attr::whereIn('id',$attrIds)->get();
        }
        // 返回商品和属性数组
        return [
            'goods'=>$goods,
            'attrs'=>$attrs
        ];
    }

    public function selectGoods($goodsIds,$attrArr){
        // 获取商品id所对应的所有货品数据
        $huopins=Huopin::whereIn('goods_id',$goodsIds)->get();
        // 定义空数组,用于存储商品id
        $newgoodsIds=[];
        // 循环货品数据
        foreach ($huopins as $huopin){
            // 合并并且去重货品属性
            $newArr=array_unique(array_merge($huopin['attrs'],$attrArr));
            // 判断是否货品包含当前属性
            if($newArr==$huopin['attrs']){
                // 将商品id存入数组
                $newgoodsIds[]=$huopin['goods_id'];
            }
        }
        // 去重商品id
        $newgoodsIds=array_unique($newgoodsIds);
        // 获取所有商品数据
        $goods=Goods::whereIn('id',$newgoodsIds)->get();
        return $goods;
    }

}
